// pages/game/end.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    isfb:false,
    fbAnimate: false,
    playshare:false,
    userInfo:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.fetchEnd(options.id);
    wx.showShareMenu({
      withShareTicket: true
    });
    this.setData({
      userInfo:wx.getStorageSync('userInfo'),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _this = this;
    var title = '低调的炫耀，是我最后的倔强';
    var shareimg='../../images/share.png';
    if (this.data.playshare == true) {
      title = '[' + this.data.userInfo.nickName +'@我]，那些年学过的成语，还记得吗？快来挑战';
      shareimg ='../../images/share3.png';
    }
    return {
      title: title,
      imageUrl: shareimg,
      path: '/pages/index/index',
      success:(res)=> {
        //分享翻倍

        if (res.shareTickets != undefined && _this.data.playshare == true) {
          this.setData({
            fbAnimate: true,
          });
          setTimeout(() => {
            this.setData({
              isfb: false,
              fbAnimate: false,
            });
            wx.redirectTo({
              url: '/pages/game/play?is_double=1',
            })

          }, 1000);
        } else {
          wx.showToast({
            title: '分享到群才可以',
            icon: 'none',
          })
          _this.setData({
            playshare: false,
          })
        }

      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          icon: 'none',
        })
      }
    }
  
  },
  fetchEnd:function(id){
    wx.request({
      url: app.globalData.api + '/board2/end.html&token=' + wx.getStorageSync('token'),
      method: 'post',
      data: {
        board_id: id,
      },
      success: (res) => {
        this.setData({
          detail:res.data.data,
        })

      }
    })
  },
  play:function(){
    wx.redirectTo({
      url: '/pages/game/play',
    })
    // this.setData({
    //   isfb: true,
    //   playshare: true,
    // })
  },
  openPH: function () {
    wx.redirectTo({
      url: '/pages/game/rank',
    })
  },
  gohome:function(){
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  closefb: function () {
    this.setData({
      isfb: false,
      playshare: false,
    })
  },
})